﻿
@{
    Layout = null;
}

<link href="~/libs/layui-v2.9.21/layui-v2.9.21/layui/css/layui.css" rel="stylesheet" />

<form class="layui-form" action="">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">工单编号</label>
            <div class="layui-input-inline layui-input-wrap">
                <input type="text" id="workshopCode" name="workshopCode" lay-verify="required" placeholder="请输入工单编码" class="layui-input">
            </div>
            <div class="layui-form-mid">
                <input type="checkbox" lay-skin="switch" lay-filter="switchTest">自动生成
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">工单名称</label>
        <div class="layui-input-block">
            <input type="text" name="workshopName" lay-verify="required" placeholder="请输入车间名称" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">工单类型</label>
        <div class="layui-input-block">
            <select>
                <option value="">请选择</option>
                <option value="自产">自产</option>
                <option value="外协">外协</option>
                <option value="外购">外购</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">工单来源</label>
        <div class="layui-input-block layui-inline">
            <input type="radio" name="state" value="1" title="客户订单" lay-filter="workorderFromFilters">
            <input type="radio" name="state" value="2" title="库存备货" lay-filter="workorderFromFilters">
        </div>
    </div>
    <div class="layui-form-item" id="ishide1" style="display:none;">
        <div>
            <label class="layui-form-label">订单编号:</label>
            <div class="layui-input-inline">
                <input type="text" name="orderCode" placeholder="请输入订单编号" class="layui-input" style="width:240px;">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">产品编号</label>
        <div class="layui-input-block">
            <select id="productCode" lay-filter="productCode">
                <option value="">请选择产品</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">产品名称</label>
        <div class="layui-input-block">
            <input disabled type="text" id="materialName" name="head" lay-verify="required" placeholder="请选择产品" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">规格型号</label>
        <div class="layui-input-block">
            <input disabled type="text" id="specificationModel" name="specificationModel" lay-verify="required" placeholder="请选择产品" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">单位</label>
        <div class="layui-input-block">
            <input disabled type="text" id="unit" name="unit" lay-verify="required" placeholder="请选择产品" class="layui-input">
        </div>
    </div>
    
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/layui-v2.9.21/layui-v2.9.21/layui/layui.js"></script>
<script>
    layui.use(['form', 'laydate'], function(){
      var form = layui.form;
      var layer = layui.layer;
      var laydate = layui.laydate;

      // 指定开关事件,生成车间编码
      form.on('switch(switchTest)', function(data){
          if(data.elem.checked){
              var random=Math.floor(Math.random()*10)+'';
              for(var i=0;i<2;i++){
                  random+=Math.floor(Math.random()*10);
              }
              random='WS'+random;
              $("#workshopCode").val(random);
          }else{
              $('input[name="workshopCode"]').val('');
          }
      });

      //如果来源类型为客户订单，则显示订单编号输入框、客户编号下拉框、客户名称输入框
      form.on('radio(workorderFromFilters)', function (data) {
        var value=data.value;
        if (data.value == '1') {
            $('#ishide1').show();
            $('#ishide2').show();
            $('#ishide3').show();
        }
        else {
            $('#ishide1').hide();
            $('#ishide2').hide();
            $('#ishide3').hide();
            //为订单编号输入框、客户编号、客户名称赋值
            $('input[name="orderCode"]').val('');
            $('select[name="userCode"]').val();
            $('input[name="userName"]').val('');
        }
      });

      //绑定物料编码下拉框
      function bindmaterials(){
          $.ajax({
              url:'/Puffs/GetMartelcodes',
              type:'get',
              dataType:'json',
              success:function(res){
                  $(res.data).each(function () {
                        $("#productCode").append('<option value="' + this.materialCode + '">' + this.materialCode + '</option>')
                    })
                    layui.form.render();
              }
          })
      }

      //根据客户编号获取客户名称
      form.on('select(productCode)', function (data) {
          var value = data.value;
          $.ajax({
              url: "/Puffs/GetByidMaterials",
              Type: "get",
              dataType: "json",
              data: { materialCode: value },
              async: false,
              success: function (res) {
                  console.log(res)
                  $('input[name="specificationModel"]').val(res.data.specificationsModels)
              }
          })
      })

     $(function(){
        bindmaterials()
     })

      // 提交事件
      form.on('submit(demo1)', function(data){
        var field = data.field; // 获取表单字段值
        $.ajax({
            url:'/Puffs/AddWorkdatas',
           data:field,
           type:'post',
           dataType:'json',
           success:res=>{
               if (res > 0) {
                  //layer信息提示
                  layer.alert('添加成功');//下面是关键代码
                  var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                  window.parent.location.reload(); //刷新父页
                  parent.layer.close(index); //再执行关闭
                  setTimeout(function () { parent.layer.close(index) }, 30000);//延迟s
               }
               else {
                  layer.alert("添加失败！");
               }
           }
        })
        return false; // 阻止默认 form 跳转
      });
    });
</script>